package com.zzx.bms.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zzx.bms.core.util.Page;
import com.zzx.bms.domain.Book;
import com.zzx.bms.domain.Journal;
import com.zzx.bms.domain.Role;
import com.zzx.bms.service.IBookService;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

/**
 * Created by SamZhao on 2016/12/6.
 */
@Controller
@RequestMapping("/book")
public class BookController {

    private static Logger logger = Logger.getLogger(BookController.class);

    @Autowired
    private IBookService bookService;

    @RequestMapping(value = "/addBook")
    public String addBook(Model model){
        return "book/addBook";
    }

    @RequestMapping("/saveBook")
    public @ResponseBody String saveBook(Book book) throws Exception {
        int flag = bookService.saveBook(book);
        if(flag > 0)
            return "success";
        else
            return "fail";
    }

    @RequestMapping("/editBook")
    public String editBook(String id, Model model) throws Exception {
        Book book = bookService.findBookById(id);
        model.addAttribute("book", book);
        return "book/editBook";
    }

    @RequestMapping("/showBook")
    public String showBook(String id, Model model) throws Exception {
        Book book = bookService.findBookById(id);
        model.addAttribute("book", book);
        return "book/showBook";
    }

    @RequestMapping("/checkBook")
    public @ResponseBody String checkBook(Book book) throws Exception {
        int flag = bookService.checkBook(book);
        if(flag > 0)
            return "success";
        else
            return "fail";
    }

    @RequestMapping("/list")
    public String bookList(Book book, Page page, Model model) throws Exception {

        List<Book> list = bookService.findBookList(book);//查询

        model.addAttribute("bookList", list);
        return "book/bookList";
    }

    @RequestMapping("/deleteBook")
    public @ResponseBody String deleteBook(Book role, String id) throws Exception {
        int flag = bookService.deleteBookById(id);
        if(flag > 0)
            return "success";
        else
            return "fail";
    }

    @RequestMapping("/deleteJournal")
    public @ResponseBody String deleteJournal(Journal role, String id) throws Exception {
        int flag = bookService.deleteJournalById(id);
        if(flag > 0)
            return "success";
        else
            return "fail";
    }


    @RequestMapping(value = "/addJournal")
    public String addJournal(Model model){
        return "book/addJournal";
    }

    @RequestMapping("/saveJournal")
    public @ResponseBody String saveJournal(Journal book) throws Exception {
        int flag = bookService.saveJournal(book);
        if(flag > 0)
            return "success";
        else
            return "fail";
    }

    @RequestMapping("/editJournal")
    public String editJournal(String id, Model model) throws Exception {
        Journal book = bookService.findJournalById(id);
        model.addAttribute("book", book);
        return "book/editJournal";
    }

    @RequestMapping("/showJournal")
    public String showJournal(String id, Model model) throws Exception {
        Journal book = bookService.findJournalById(id);
        model.addAttribute("book", book);
        return "book/showJournal";
    }

    @RequestMapping("/checkJournal")
    public @ResponseBody String checkJournal(Journal book) throws Exception {
        int flag = bookService.checkJournal(book);
        if(flag > 0)
            return "success";
        else
            return "fail";
    }

    @RequestMapping("/journalList")
    public String journalList(Journal book, Page page, Model model) throws Exception {

        List<Journal> list = bookService.findJournalList(book);//查询

        model.addAttribute("journalList", list);
        return "book/journalList";
    }

}
